package com.tgy.reflection;

/***
 * @ClassName: TestClass
 * @Description: 测试获取Class的方式
 * @Auther: 送一一块二向箔
 * @Date: 14:22 2021/12/1
 * @version : V1.0
 */
public class TestClass {
    public static void main(String[] args) throws ClassNotFoundException {
        Person person = new Student();
        System.out.println("这个人是："+person.getName());


        //方式一 通过类的class方法  推荐
        Class<Student> c1 = Student.class;
        System.out.println(c1.hashCode());
        //方式二，通过对象获得
        Class c2 = person.getClass();
        System.out.println(c2.hashCode());
        //方式三、forname获得
        Class c3 = Class.forName("com.tgy.reflection.Student");
        System.out.println(c3.hashCode());
        //方式四：基本内置类型的包装类都有一个Type属性
        Class<Integer> c4 = Integer.TYPE;
        System.out.println(c4);


        //获得父类类型
        Class c5 = c1.getSuperclass();
        System.out.println(c5);

    }
}
class Person {
    private String name;

    public Person() {
    }

    public Person(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "person{" +
                "name='" + name + '\'' +
                '}';
    }

    public String getName() {
        return name;
    }

    public Person setName(String name) {
        this.name = name;
        return this;
    }

}
class Student extends Person{
    public Student(){
        this.setName("学生");
    }
}
class Teacher extends Person{
    public Teacher(){
        this.setName("老师");
    }
}
